﻿using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Rendering;

namespace GameToolkit
{
    public class LightingClip : PlayableAsset
    {
        [SerializeField]
        ColorParameter m_LightColor = new ColorParameter(Color.white);
        public ColorParameter color => m_LightColor;

        [SerializeField]
        FloatParameter m_LightIntensity = new FloatParameter(1f);
        public FloatParameter intensity => m_LightIntensity;

        [SerializeField]
        FloatParameter m_ShadowStrength = new FloatParameter(1f);
        public FloatParameter shadowStrength => m_ShadowStrength;

        public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
        {
            var playable = ScriptPlayable<LightingParameter>.Create(graph);
            var behaviour = playable.GetBehaviour();
            behaviour.clipData = this;
            return playable;
        }

        public class LightingParameter : PlayableBehaviour
        {
            public LightingClip clipData { get; set; }
            LightingTrack.LightingBehaviour mMixer;

            public override void OnGraphStart(Playable playable)
            {
                var track = playable.GetOutput(0);
                mMixer = ((ScriptPlayable<LightingTrack.LightingBehaviour>)track).GetBehaviour();
            }

            public override void ProcessFrame(Playable playable, FrameData info, object playerData)
            {
                mMixer.Override(clipData, info.weight);                
            }
        }
    }
}
